import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: torstein
 * Date: 3/20/12
 * Time: 12:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class Relations {
    private Quiver  quiver;
    private ArrayList<Relation> relations = new ArrayList<Relation>();

    public Relations(Quiver quiver) {
        this.quiver = quiver;
    }

    public ArrayList<Relation> getRelations() {
        return relations;
    }

    public Relation get(int index) {
        if (index < 0 || index >= relations.size()) {
            return null;
        }
        return relations.get(index);
    }

    public void setRelations(ArrayList<Relation> relations) {
        this.relations = relations;
    }

    public void remove (Relation relation) {
        relations.remove(relation);
    }

    public void add (Relation r) {
        relations.add(r);
    }

    public boolean contains(Relation relation) {
        return relations.contains(relation);
    }

    public int size() {
        return relations.size();
    }
    
    public String toGAPCode () {
        String relationString = "";
        int i = 0;
        for (Relation r : relations) {
                if (i != 0) {
                    relationString += ",";
                }
                relationString += r.toString();
                i++;
        }
        return String.format("%sRels := [%s]", quiver.getName(), relationString);
    }

/*    public static Relations fromGAPCode(Quiver quiver, String gapCode) {
        //todo : Make work.
        Configuration config = Configuration.getDefaultConfiguration();
        Relations relations = new Relations(quiver);
        Pattern p = Pattern.compile("((?:\\S+Rels\\:=)?)\\(\\[(.*?)\\]\\)");



 //       QRels:=[a*a-c*b,b*a*c-b*a*a*c,a*a*a*c,b*a*a*a];




        Matcher m = p.matcher(gapCode.replace(" ", "")); // should remove all
        // whitespace
        if (m.find()) {
            if (!m.group(1).isEmpty()) {
                relations.setQuiverName(m.group(1).substring(0, m.group(1).length() - 6));
            }
            if (!m.group(2).isEmpty()) {
                for (String relationsString : m.group(2).split(",")) {
                    if (relationsString.contains("-")) {
                       for (String relationString : relationsString.split("-")) {
                          ArrayList<Arrow> positive = new ArrayList<Arrow>();
                          ArrayList<Arrow> negative = new ArrayList<Arrow>();
                       }
                    }
                }
            }
        }
        return relations;
    }*/
}
